package practice;

import java.sql.*;
public class ConnectToDB{
  public static void main(String[] args){
    Connection myCon;
    Statement myStmt;
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      // Connect to an instance of mysql with the follow details:
      // machine address: ensembldb.ensembl.org
      // database       : homo_sapiens_core_47_36i
      // table			: gene_attrib
      // user name      : anonymous
      // password       : 
      // mysql -hensembldb.ensembl.org -uanonymous 
      myCon = DriverManager.getConnection(
              "jdbc:mysql://ensembldb.ensembl.org:3306/homo_sapiens_core_47_36i",
              "anonymous","");
      myStmt = myCon.createStatement();
      // Define query
      ResultSet result = myStmt.executeQuery("select * from gene_attrib where gene_id=33786 or gene_id=33785");
      // Get number of columns of the table
      int numberOfColumns = result.getMetaData().getColumnCount();
      
      // Define string array to store rows
      String[] records = new String[numberOfColumns];
      // Poll the returned records
      while (result.next()){
      // Output one record
    	  for(int i=0; i<numberOfColumns;i++)
          {		
    		  //Value starts from ONE
    		  records[i]= result.getString(i+1);
        	  System.out.print(records[i]+" ");
          }
    	  System.out.print("\n");
      }

      myCon.close();
    }
    catch (Exception sqlEx){
      System.err.println(sqlEx);
    }
  }
}